package net.qhzw.vip.qiyu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import net.qhzw.vip.qiyu.entity.Information;
import net.qhzw.vip.qiyu.result.JsonResult;
import net.qhzw.vip.qiyu.service.InformationService;
import net.qhzw.vip.qiyu.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/information")
public class MallInformationController {

    @Autowired
    private InformationService informationService;

    @GetMapping("/info")
    public JsonResult<Information> findById() {
        Long shopId = SecurityUtils.getLoginUser().getShopId();
        Information information = informationService.getOne(new LambdaQueryWrapper<Information>()
                .eq(Information::getShopId, shopId).last("LIMIT 1"));
        return JsonResult.ok(information);
    }

    @PostMapping("/edit")
    public JsonResult edit(@RequestBody Information information) {
        Long shopId = SecurityUtils.getLoginUser().getShopId();
        information.setShopId(shopId);
        if (information.getId() != null) {
            informationService.updateById(information);
        } else {
            informationService.save(information);
        }
        return JsonResult.ok();
    }

}
